package com.faxsun.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.transaction.annotation.Transactional;

import mtons.modules.pojos.Page;

import com.faxsun.core.persist.dao.LiveDao;
import com.faxsun.core.persist.dao.WeChatMsgDao;
import com.faxsun.core.persist.entity.LivePO;
import com.faxsun.core.persist.entity.WechatMsgPO;
import com.faxsun.core.service.WechatService;

@Transactional
public class WechatServiceImpl implements WechatService {

	@Autowired
	WeChatMsgDao wechatMsgDao;

	@Autowired
	LiveDao liveDao;

	@Override
	@Transactional
	public void save(WechatMsgPO po) {
		// TODO Auto-generated method stub
		wechatMsgDao.saveOrUpdate(po);
	}

	@Override
	@Transactional
	public List<WechatMsgPO> listRecent(Page page, String topic) {
		return wechatMsgDao.listRecent(page, topic);
	}

	@Override
	public void pagingRecentEvent(Page page, Date current) {
		liveDao.listRecent(page, current);
	}

	@Override
	public void pagingFinishedEvent(Page page, Date current) {
		liveDao.listFinished(page, current);
	}

	@Override
	public void pagingActiveEvent(Page page, Date current) {
		liveDao.listActive(page, current);
	}

	@Override
	@Cacheable(value = "wechatCache", key = "'findEventByTopic?topic='+#topic")
	@Transactional
	public LivePO findEventByTopic(String topic) {
		return liveDao.findByTopic(topic);
	}

	@Override
	@Caching(evict = { @CacheEvict(value = "wechatCache", allEntries=true) })
	@Transactional
	public void saveEvent(LivePO livepo) {
		liveDao.saveOrUpdate(livepo);
	}

	@Override
	@Cacheable(value = "wechatCache", key = "'findEventById?id='+#id")
	@Transactional
	public LivePO findEventById(long id) {
		return liveDao.get(id);
	}

	@Override
	@Cacheable(value = "wechatCache", key = "'pagingAllEvent?pageNo='+#page.pageNo+'&size='+#page.maxResults")
	@Transactional
	public Page pagingAllEvent(Page page, boolean isAdminEnd) {
		List<LivePO> result = liveDao.listAllEvent(page, isAdminEnd);
		page.setResults(result);
		return page;
	}

	@Override
	public void pagingPresentChannels(Page page) {
		Date current = new Date(System.currentTimeMillis());

		this.liveDao.listPresent(page, current);
	}

	@Override
	public void pagingPastChannels(Page page) {
		Date current = new Date(System.currentTimeMillis());
		this.liveDao.listPast(page, current);
	}

	@Override
	public void pagingFutureChannels(Page page) {
		Date current = new Date(System.currentTimeMillis());
		this.liveDao.listFuture(page, current);
	}

	@Override
	@Transactional
	@Cacheable(value = "wechatCache", key = "'cntAllLive'")
	public int cntAllLive() {
		return this.liveDao.countAllLive();
	}

}
